package com.itheima.springboottilasmanager.survice.deptsurvice;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.springboottilasmanager.Exception.DeptNotAllowDeleteException;
import com.itheima.springboottilasmanager.mapper.ClazzMapper;
import com.itheima.springboottilasmanager.mapper.EmpMapper;
import com.itheima.springboottilasmanager.mapper.StudentMapper;
import com.itheima.springboottilasmanager.pojo.domain.Clazz;
import com.itheima.springboottilasmanager.pojo.domain.Emp;
import com.itheima.springboottilasmanager.pojo.domain.PageResult;
import com.itheima.springboottilasmanager.pojo.domain.Student;
import com.itheima.springboottilasmanager.pojo.vo.queryString;
import com.itheima.springboottilasmanager.survice.ClazzSurvice;
import com.itheima.springboottilasmanager.survice.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ClazzImp extends ServiceImpl<ClazzMapper, Clazz> implements ClazzSurvice {
@Autowired
private EmpMapper empMapper;
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentService studentService;
    @Override
    public boolean addClazz(Clazz clazz) {
        return clazzMapper.insert(clazz)>0;
    }

    @Override
    public PageResult<Clazz> findall(queryString querystring) {
        Page<Clazz> page = Page.of(querystring.getPage(), querystring.getPageSize());
        LambdaQueryWrapper<Clazz> wrapper = new LambdaQueryWrapper<Clazz>()
                .like(querystring.getName() != null, Clazz::getName, querystring.getName());
        // .between(...) 可根据需要打开

        page = page(page, wrapper); // 先查询分页数据
        List<Clazz> clazzList = page.getRecords();

        if (CollectionUtil.isNotEmpty(clazzList)) {
            // 批量查班主任
            List<Integer> masterIds = clazzList.stream()
                    .map(Clazz::getMasterId)
                    .filter(Objects::nonNull)
                    .distinct()
                    .toList();
            List<Emp> empList = empMapper.selectBatchIds(masterIds);

            clazzList.forEach(clazz -> {
                if (clazz.getMasterId() != null) {
                    empList.stream()
                            .filter(emp -> emp.getId().equals(clazz.getMasterId()))
                            .findFirst()
                            .ifPresent(emp -> clazz.setMasterName(emp.getName()));
                }
                clazz.panduan(); // 设置班级状态
            });
        }

        return new PageResult<>(page.getTotal(), clazzList);
    }


//    public PageResult<Clazz> findall(queryString querystring) {
//
//      Page<Clazz> page =  Page.of(querystring.getPage(),querystring.getPageSize());
//        LambdaQueryWrapper<Clazz> wrapper = new LambdaQueryWrapper<Clazz>()
//                .like(querystring.getName() != null, Clazz::getName, querystring.getName());
////                .between(querystring.getBegin()!=null,Clazz::getBeginDate, querystring.getBegin(), querystring.getEnd());
//
//        page = page(page,wrapper);
//        List<Clazz> Clazzlist = page.getRecords();
//        if (CollectionUtil.isNotEmpty(Clazzlist)) {
//          List<Integer> Clazzids = Clazzlist.stream().map(Clazz::getId).toList();
//          List<Emp> empList = empMapper.selectBatchIds(Clazzids);
//          if (CollectionUtil.isNotEmpty(empList)){
//              Clazzlist.forEach(clazz->
//                      {empList.stream().filter(emp->emp.getId().equals(clazz.getMasterId())).findFirst().ifPresent(emp->clazz.setMasterName(emp.getName()));
//                      clazz.panduan();
//                      }
//              );
//          }
//
//         }
//    return new PageResult<>(page.getTotal(),Clazzlist);}



    @Override
    public boolean deleteClazz(Integer id) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getClazzId, id);
      long count = studentService.count(queryWrapper); // 或 studentMapper.selectCount(queryWrapper)

        if (count > 0) {
            throw new DeptNotAllowDeleteException("此班级有学生，不能删除");
        }
        return removeById(id);
    }
    @Override
    public boolean updateClazz(Clazz list) {
        // 先计算业务字段
        list.setUpdateTime(LocalDateTime.now());
         // ⚡ 注意放在 update 之前

        // 执行更新
        return updateById(list);
    }



    @Override
    public Clazz selectById(Integer id) {
  Clazz clazzList =   clazzMapper.selectById(id);

        return clazzList;
    }
    @Override
    public List<Clazz> selectAll() {
   List<Clazz> clazzList = clazzMapper.selectAll();
  return clazzList;
    }
}

